package com.glodon.paas.storage.ali;

import com.aliyun.openservices.ClientException;
import com.aliyun.openservices.oss.OSSException;
import com.glodon.paas.storage.BulkStorageException;
import com.glodon.paas.storage.Part;
import com.glodon.paas.storage.WriteResult;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.testng.Assert;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Ignore
public class OSSBulkStorageIT {
    private final static String KEY1 = "junit/test1.bin";
    private final static String KEY2 = "junit/test2.bin";
    private final static String KEY3 = "junit/test3.bin";
    private final static int LENGTH = 32;
    private final static String bucketName = "glodon_paas";
    private final static String accessId = "frnaiwrcfz56xjbhe78fl4go";
    private final static String accessKey = "R4eDSoZQzthCVCEuATjZUQ4Hheo=";
    private final static String endpoint = "http://storage.aliyun.com/";

    private OSSBulkStorage storage;
    private OSSObjectGroupOperation objectGroupOperation;

    @Before
    public void setUp() throws Exception {

        storage = new OSSBulkStorage(endpoint, accessId, accessKey, bucketName);
        objectGroupOperation = OSSObjectGroupOperation.createInstance(endpoint, accessId, accessKey);
    }

    @Test
    public void testCreateObjectGroup() throws FileNotFoundException, BulkStorageException, OSSException, ClientException {
        byte[] content = new byte[LENGTH];
        for (byte i = 0; i < content.length; i++) {
            content[i] = i;
        }
        WriteResult result1 = storage.write(KEY1, new ByteArrayInputStream(content), content.length);
        WriteResult result2 = storage.write(KEY2, new ByteArrayInputStream(content), content.length);


        Part part1 = new Part();
        part1.setIndex(1);
        part1.setKey(result1.getKey());
        part1.setETag(result1.getETag());
        part1.setSize(content.length);

        Part part2 = new Part();
        part2.setIndex(2);
        part2.setKey(result2.getKey());
        part2.setETag(result2.getETag());
        part2.setSize(content.length);

        List<Part> parts = new ArrayList<Part>();
        parts.add(part1);
        parts.add(part2);

        CreateObjectGroupResult result = objectGroupOperation.createObjectGroup(bucketName, KEY3, parts, null);
        System.out.println("KEY: " + result.getKey());
        System.out.println("SIZE: " + result.getSize());
        System.out.println("ETAG: " + result.getETag());
    }

    @Test
    public void testRead() throws BulkStorageException, IOException {
        byte[] content = new byte[LENGTH];
        InputStream input = storage.read(KEY1).getInputStream();
        input.read(content);
        input.close();

        for (byte i = 0; i < content.length; i++) {
            Assert.assertEquals(content[i], i);
        }
    }

    @Test
    public void testDelete() throws BulkStorageException {
        storage.delete(KEY1);
        storage.delete(KEY2);
        storage.delete(KEY3);
    }

}
